/* 任务管理
---------------------------------------------------------------- */
import {
  GetTaskInfoParam,
  TaskInfo,
  HandleTaskInfo,
  TaskDetailInfo,
  TaskDialogueInfo,
  TaskProcessInfo,
  TaskCallResultInfo,
  GetCallRecordParams,
  TaskCallRecordInfo,
  ColInfoResponse,
  CallAnalysisConversionInfo,
  CallAnalysisDurationInfo,
  CallStrategyDetail,
  CallSummaryDetail,
  RepeatTaskParam,
} from 'admin/schema/Task';
import $http from '../../../servers/http';
import { Result } from '../../schema/Result';

/**删除任务 */
export const delTask = (taskId: number): Promise<Result<null>> => {
  return $http.delete({
    url: `/icms/taskmanage/tasks/${taskId}`,
  });
};

/**查询任务列表 */
export const getTaskList = (data: GetTaskInfoParam): Promise<Result<TaskInfo[]>> => {
  console.log('查询任务列表条件：', data);
  return $http.get({
    url: '/icms/taskmanage/tasks',
    data,
  });
};
/**创建下载任务-任务列表-导出 */
export const createTaskListDownloadTask = (data: GetTaskInfoParam): Promise<Result<null>> => {
  console.log('导出任务列表条件：', data);
  return $http.post({
    url: '/icms/taskmanage/tasks/export',
    data,
  });
};

/**获取任务详情-重呼策略  */
export const getCallStrategyDetail = (taskId: number): Promise<Result<CallStrategyDetail[]>> => {
  return $http.get({
    url: `/icms/taskmanage/tasks/${taskId}/strategies`,
  });
};
/**任务重呼  */
export const repeatCallTask = (taskId: number, data: RepeatTaskParam): Promise<Result<null>> => {
  return $http.post({
    url: `/icms/taskmanage/tasks/${taskId}/repeatcall`,
    data,
  });
};
/**新建任务 */
export const addTask = (data: HandleTaskInfo): Promise<Result<null>> => {
  return $http.post({
    url: '/icms/taskmanage/tasks',
    data,
  });
};

/**启动任务 */
export const startTask = (taskId: number): Promise<Result<null>> => {
  return $http.put({
    url: `/icms/taskmanage/tasks/start/${taskId}`,
  });
};

/**暂停任务 */
export const stopTask = (taskId: number): Promise<Result<null>> => {
  return $http.put({
    url: `/icms/taskmanage/tasks/pause/${taskId}`,
  });
};

/**获取单个任务详情 */
export const getTaskDetail = (taskId: number): Promise<Result<TaskDetailInfo>> => {
  return $http.get({
    url: `/icms/taskmanage/tasks/${taskId}`,
  });
};

/** 获得某次会话的历史信息 */
export const getDialogue = (
  data: {
    callRecordId: string;
    time: string;
  },
  type?: string
): Promise<Result<TaskDialogueInfo[]>> => {
  return $http.get({
    url: type
      ? '/robotmanage/flowdebugrecords/callrecords/dialogue'
      : '/icms/taskmanage/tasks/callrecords/dialogue',
    data,
  });
};

/**获取任务进度信息 */
export const getTaskProcess = (taskId: number): Promise<Result<TaskProcessInfo>> => {
  return $http.get({
    url: `/icms/taskmanage/tasks/${taskId}/queryprogress`,
  });
};

/**获取任务外呼结果信息 */
export const getCallResult = (taskId: number): Promise<Result<TaskCallResultInfo[]>> => {
  return $http.get({
    url: `/icms/taskmanage/tasks/${taskId}/callresultstatistics`,
  });
};

/**获取任务通话记录列表信息 */
export const getCallRecordList = (
  taskId: number,
  data: GetCallRecordParams
): Promise<Result<TaskCallRecordInfo[]>> => {
  // get请求，需要转换呼叫结果数组
  let callStatusStr = '';
  data.callStatus?.forEach(status => {
    callStatusStr += `callStatus=${status}&`;
  });
  delete data.callStatus;
  return $http.get({
    url: `/icms/taskmanage/tasks/${taskId}/callrecords?${callStatusStr}`,
    data,
  });
};
/** 获取任务列表列头 */
export const getCallTaskCol = (): Promise<Result<ColInfoResponse[]>> => {
  return $http.get({
    url: '/icms/taskmanage/tasks/column',
  });
};
/** 编辑任务列表列头 */
export const modifyCallTaskCol = (data: ColInfoResponse[]): Promise<Result<null>> => {
  return $http.put({
    url: '/icms/taskmanage/tasks/column',
    data,
  });
};

/** 获取通话记录列表列头 */
export const getCallRecordCol = (): Promise<Result<ColInfoResponse[]>> => {
  return $http.get({
    url: '/icms/taskmanage/tasks/callrecords/column',
  });
};

/** 编辑通话记录列表列头 */
export const modifyCallRecordCol = (data: ColInfoResponse[]): Promise<Result<null>> => {
  return $http.put({
    url: '/icms/taskmanage/tasks/callrecords/column',
    data,
  });
};

/**获取任务详情-通话分析-转化率分析 */
export const getCallAnalysisConversion = (
  taskId: number
): Promise<Result<CallAnalysisConversionInfo>> => {
  return $http.get({
    url: `/icms/taskmanage/tasks/${taskId}/conversionanalysis`,
  });
};

/**获取任务详情-通话分析-时长分析  */
export const getCallAnalysisDuration = (
  taskId: number,
  data: {
    intervalTime: number;
    intervalCount: number;
  }
): Promise<Result<CallAnalysisDurationInfo>> => {
  return $http.get({
    url: `/icms/taskmanage/tasks/${taskId}/calldurationanalysis`,
    data,
  });
};

/**获取通话记录详情-通话小结信息 */
export const getCallSummaryDetail = (callRecordId: string): Promise<Result<CallSummaryDetail>> => {
  return $http.get({
    url: `/seats/callrecords/callsummary/${callRecordId}`,
  });
};

/**
 * 拦截通话
 */
export const interceptCall = (
  callCenterId: string,
  data: { intercepter: string }
): Promise<Result<null>> => {
  return $http.post({
    url: `/icms/taskmanage/tasks/callrecords/intercept/${callCenterId}`,
    data,
  });
};

/**
 * 下载通话记录录音
 */
export const downloadCallRecord = (callRecordId: string): Promise<Result<any>> => {
  return $http.get(
    {
      url: `/icms/taskmanage/callrecords/${callRecordId}/downloadrecord`,
    },
    {
      responseType: 'blob', // 声明返回blob格式
      onDownloadProgress: (progress: any) => {
        // 下载接口
        console.log(progress, '下载接口');
        // console.log(Math.round((progress.loaded / progress.total) * 100) + "%");
      },
    }
  );
};
